package com.demo.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo.entity.User;
import com.demo.service.MyService;

@Controller
public class MyController {

	@Resource
	private MyService myService;

	Logger logger = LoggerFactory.getLogger(getClass());
	/**
	 * 新增功能 重定向 和 转发 将对象从前台传到后台 (字符串传到后台类同，如：indexSubmit(String str))
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "indexSubmit.do", method = RequestMethod.POST)
	public String indexSubmit(User user) {
		logger.info("username = {}" ,user.getUsername());
		logger.info("password = {}" ,user.getPassword());
		logger.info("email = {}" ,user.getEmail());
		try {
			myService.insert(user);
			logger.info("info=新增成功");
		} catch (Exception e) {
			logger.error("exception:" , e);
		}
		return "forward:/queryUser.do";
	}

	/**
	 * 修改功能 将对象从前台传到后台 (字符串传到后台类同，如：updateSubmit(String str))
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public String updateSubmit(User user) {

		try {
			myService.updateUser(user);
			logger.info("修改成功！");
		} catch (Exception e) {
			logger.error("updateSubmit error",e);
		}
		return "user";
	}

	/**
	 * 删除功能 将对象从前台传到后台 (字符串传到后台类同，如：deleteSubmit(String str))
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "delete.do")
	public String deleteSubmit(String userId) {
		logger.info("userId ={}" , userId);
		try {
			myService.deleteUser(userId);
			logger.info("删除成功！");
		} catch (Exception e) {
			logger.error("deleteSubmit error",e);
		}
		return "index";
	}

	/**
	 * 字符串从后台传到前台
	 * 
	 * @param request
	 * @param response
	 * @return
	 */

	@RequestMapping(value = "user.do")
	public String indexMethod(HttpServletRequest request, String email) {

		// 这里可以有多种传参方式，欲知如何，请看下文...

		Object obj = myService.getUserByEmail(email);
		request.setAttribute("user", (User) obj);
		return "user";
	}

	/**
	 * 将对象对象传到前台 方式一：
	 * 
	 * 字符串类同
	 * 
	 */
	@RequestMapping(value = "getUser.do")
	public ModelAndView handler() {
		User user = new User();
		user.setEmail("275001477@qq.com");
		user = myService.getUser(user);
		return new ModelAndView("list_user").addObject(user);
	}

	/**
	 * 将对象对象传到前台 方式二：
	 * 
	 */
	@RequestMapping(value = "getUser2.do")
	public String handler2(Model model) {
		User user = new User();
		user.setEmail("275001477@qq.com");
		user = myService.getUser(user);
		model.addAttribute(user);
		return "list_user";
	}

	/**
	 * 将对象集合从后台传到前台 方式一：
	 * 
	 * @return
	 */
	@RequestMapping(value = "queryUser.do")
	public ModelAndView queryUser() {
		List<User> list = myService.selectList();
		return new ModelAndView("query_list").addObject("list", list);
	}

	/**
	 * 将对象集合从后台传到前台 方式二
	 * 
	 * @return
	 */

	@RequestMapping(value = "queryUser2.do")
	public String queryUser2(HttpServletRequest request) {
		List<User> list = new ArrayList<User>();

		User user1 = new User();
		user1.setEmail("1@qq.com");
		user1.setPassword("password1");
		list.add(user1);

		User user2 = new User();
		user2.setEmail("2@qq.com");
		user2.setPassword("password2");
		list.add(user2);

		User user3 = new User();
		user3.setEmail("3@qq.com");
		user3.setPassword("password3");
		list.add(user3);

		request.setAttribute("list", list);
		return "query_list";
	}

	/**
	 * 将对象集合从后台传到前台 方式三
	 * 
	 * @return
	 */
	@RequestMapping(value = "queryUser3.do")
	public String queryUser3(Model model) {

		List<User> list = myService.selectList();

		model.addAttribute("list", list);
		return "query_list";
	}

	/**
	 * 测试异常 演示日志
	 * 
	 * @return
	 */
	@RequestMapping(value = "debug.do")
	public String debug(Model model) {
		logger.info("正在执行:debug.do");
		try {
			@SuppressWarnings("unused")
			int i = 1 / 0;
		} catch (Exception e) {
			logger.error("debug",e);
		}
		return "query_list";
	}

	/**
	 * 测试异常2 演示springmvc 异常处理   有配置控制日志记录
	 */
	@RequestMapping(value = "debug2.do")
	public String debugNull() {
			throw new RuntimeException("测试异常");
	}
	/**
	 * 
	* @Title: echcacheTest 
	* @Description:缓存测试
	* @param @param user
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value = "echcacheTest.do", method = RequestMethod.GET)
	public String echcacheTest(User user) {
		logger.info("email = {}",user.getEmail());
		try {
			myService.selectList();
			logger.info("info=新增成功");
		} catch (Exception e) {
			logger.error("exception:" , e);
		}
		return "forward:/queryUser.do";
	}
	
}